package kz.home;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.UnsupportedEncodingException;

/**
 * <strong>title</strong>
 * <p> describe
 *
 * @author: Danik <br>
 * Date: 14.07.2009  <br>
 * Time: 15:04:49  <br>
 */
public class TestSomeStore extends MIDlet {
    String encoding = "UTF-8";

    String db = "someDB";
    protected void startApp() throws MIDletStateChangeException {
        //encoding = System.getProperty("microedition.encoding");

        System.out.println("encoding [" + encoding + "]");

        byte[] someBytes = {1, 2, 3, 4, 5};
        byte[] someBytes2 = {11, 12, 13, 14, 15};
        byte[] someBytes3 = null;
        try {
            someBytes3 = "тест".getBytes(encoding);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        try {
            RecordStore.deleteRecordStore(db);
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }

        try {
            RecordStore rs = RecordStore.openRecordStore(db, true);
            int id = rs.addRecord(someBytes, 0, someBytes.length);
            System.out.println("1 id [" + id + "]");
            System.out.println("1 num record [" + rs.getNumRecords() + "]");
            System.out.println("1 size [" + rs.getSize() + "]");
            System.out.println("1 size [" + rs.getSizeAvailable() + "]");
            System.out.println("");

            int id2 = rs.addRecord(someBytes2, 0, someBytes.length);
            System.out.println("2 id[" + id2 + "]");
            System.out.println("2 num record [" + rs.getNumRecords() + "]");
            System.out.println("2 size [" + rs.getSize() + "]");
            System.out.println("2 size [" + rs.getSizeAvailable() + "]");
            System.out.println("");

            if (someBytes3 != null) {
                int id3 = rs.addRecord(someBytes3, 0, someBytes3.length);
                System.out.println("3 id[" + id3 + "]");
                System.out.println("3 num record [" + rs.getNumRecords() + "]");
                System.out.println("3 size [" + rs.getSize() + "]");
                System.out.println("3 size [" + rs.getSizeAvailable() + "]");
            }

            rs.closeRecordStore();
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }

        System.out.println("read-----------------");
        read();

    }

    private void read() {
        try {
            RecordStore rs = RecordStore.openRecordStore(db, true);
            System.out.println(" num record [" + rs.getNumRecords() + "]");
            for (int i = 1; i <= rs.getNumRecords(); i++) {
                byte[] somestore = rs.getRecord(i);
                System.out.println("byte [" +getBytes(somestore)+ "]");
                if (i == 3) {
                    encoding = "CP1251";

                    System.out.println("[" + new String(somestore, encoding) + "]");
                }
                System.out.println("");
            }
            rs.closeRecordStore();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void read_v2() {
        try {
            RecordStore rs = RecordStore.openRecordStore(db, true);
            
            System.out.println(" num record [" + rs.getNumRecords() + "]");
            for (int i = 1; i <= rs.getNumRecords(); i++) {
                byte[] data = rs.getRecord(i);
                DataInputStream is = new DataInputStream(new ByteArrayInputStream(data));
                System.out.println("byte [" +getBytes(data)+ "]");
                if (i == 3) {
                    String rg = is.readUTF();
                    System.out.println("[" + rg + "]");
                }

                System.out.println("");
            }
            rs.closeRecordStore();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String getBytes(byte[] somestore) {
        String result = "";
        for (int i = 0; i < somestore.length; i++) {
            result += " " + somestore[i];
        }
        return result;
    }

    protected void pauseApp() {}

    protected void destroyApp(boolean b) throws MIDletStateChangeException {}
}
